Data Structures and Algorithms
GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Analysis of Common Loops

Increasing counter

Python

for i in range(0, n, c):
    # some constant work

JavaScript

for (let i = 0; i < n; i += c) {
  // some constant work
}
  • Example: for n = 10 and c = 2, it will run 5 times (0, 2, 4, 6, 8).
  • Time complexity for this loop is Θ(nc)\Theta(\lfloor \frac{n}{c} \rfloor) . Ignoring constants, it’s Θ(n)\Theta(n) .

Decreasing counter

Python

for i in range(n, 0, c) # where c is negative value
    # some constant work

JavaScript

for (let i = n; i > 0; i -= c) {
  // some constant work
}
  • Example: for n = 10 and c = 2, it will run 5 times (10, 8, 6, 4, 2).
  • Time complexity for this loop is Θ(nc)\Theta(\lceil \frac{n}{c} \rceil) . Ignoring constants, it’s Θ(n)\Theta(n) .

Counter getting multiplied in each iteration

Python

i = 1
    while i < n:
        # some constant work
        i *= c

JavaScript

for (let i = 1; i < n; i *= c) {
  // some constant work
}
  • Example: For n = 32 and c = 2, it will be executed 5 times 1, 2, 4, 8, 16. For n = 33 and c = 2, it will be executed 6 times 1, 2, 4, 8, 16, 32. Generalizing, it runs for 1,c,c2,c3,...,ck11, c, c^2, c^3, ..., c^{k-1} i.e. it runs kk times from 11 to k1k-1 .

    ck1<nk1<logcnk<logcn+1 c^{k-1} < n \\ k-1 < \log_c n \\ k < \log_c n + 1

    So, the loop is going to run logcn+1\log_c n + 1 times.

  • Time complexity for this loop is Θ(logn)\Theta(\log n) .

  • Note that base of the log doesn’t matter, since bases can be converted by simple multiplication or division operations and in asymptotic analysis constants are ignored.

Counter getting divided in each iteration

Python

i = n
    while i > 1:
        # some constant work
        i //= c # // is integer division

JavaScript

for (let i = n; i > 1; i /= c) {
  // some constant work
}
  • Example:
    For n = 32 and c = 2, it will be executed 5 times 32, 16, 8, 4, 2. For n = 33 and c = 2, it will be executed 6 times 33, 16, 8, 4, 2`.
  • Time complexity for this loop is Θ(logn)\Theta(\log n) .

Counter raised to some power in each iteration

Python

i = 2
    while i < n:
        # some constant work
        i = pow(i, c)

JavaScript

for (let i = 2; i < n; i = Math.pow(i, c)) {
  // some constant work
}
  • Example: For c = 2 and n = 32 it’s going to run for 2,22,(22)22, 2^2, {(2^2)}^2 i.e. 2, 4, 16.

    Let’s find out the number of times the loop runs:

    2,2c,(2c)c2,2c,2c2,...2ck1k is the number of times it runs2ck1<nck1<log2nk1<log2log2nk<log2log2n+1 2, 2^c, {(2^c)}^c \\ 2, 2^c, 2^{c^2}, ...2^{c^{k-1}} \text{\footnotesize k is the number of times it runs} \\ 2^{c^{k-1}} < n \\ c^{k-1} < \log_2 n \\ k - 1 < \log_2 \log_2 n \\ k < \log_2 \log_2 n + 1
  • Time complexity of this loop is Θ(loglogn)\Theta(\log\log n) .

Sequential loops

Python

def fun(n):
    for i in range(n):          # 𝛳(n)
        # some constant work
    i = 0
    while i < n:                # 𝛳(log n)
        # some constant work
        i *= 2
    for i in range(1, 100):     # 𝛳(1)
        # some constant work

JavaScript

function fun(n) {
  for (let i = 0; i < n; i++) {
    // some constant work
  }
  i = 0;
  while(i < n) {
    i *= n;
  }
  for (i = 0; i < 100; i++) {
    // some constant work
  }

}
  • Since the work is sequential, we add the values Θ(n)+Θ(logn)+Θ(1)\Theta(n) + \Theta(\log n) + \Theta(1) .
    Ignoring lower order terms, the complexity of this function is Θ(n)\Theta(n) .

Nested loops

Python

def fun(n):
    for i in range(n):            # 𝛳(n)
        j = 0
        while j < n:              # 𝛳(log n)
            # some constant work
            j *= 2

JavaScript

function fun(n) {
  for(let i = 0; i < n; i++) {
    let j = 0;
    while(j < n) {
      j *= 2;
    }
  }
}
  • Since it’s a nested loop,we multiply the values Θ(n)Θ(logn)\Theta(n) * \Theta(\log n) .
    Therefore the complexity is Θ(nlogn)\Theta(n \log n) .

Nested loops 2

Python

def fun(n):
    for i in range(n):          # 𝛳(n)
        for j in range(n):      # 𝛳(n)
            # some constant work

JavaScript

function fun(n) {
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      // some constant work
    }
  }
}
  • The time complexity is Θ(n2)\Theta(n^2) .